//
//  CLRefreshAnimationView.m
//  DGGMobileFrameworksDemo
//
//  Created by 伍明鹏 on 2019/12/23.
//  Copyright © 2019 伍明鹏. All rights reserved.
//

#import "DLRefreshAnimationView.h"
#import <Masonry/Masonry.h>

@interface DLRefreshAnimationView()

@property (nonatomic, strong) CABasicAnimation *scaleAnimation;
@property (nonatomic, strong) CABasicAnimation *scalesSmallAnimation;
@property (nonatomic, strong) CABasicAnimation *rotationAnimation;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *circleView;
@end

@implementation DLRefreshAnimationView

- (instancetype)init{
    if (self = [super init]) {
        self.tintColor = [UIColor colorWithHex:0x10BBB8];
    }
    return self;
}

#pragma mark - actions
- (void)buildUI{
    
    [self.circleView.layer addSublayer:self.refreshLayer];
    self.lab.text = self.refreshText;
    
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.circleView];
    [self.contentView addSubview:self.lab];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerX.top.mas_equalTo(self);
       make.height.mas_equalTo(50);
    }];
    if (self.refreshTextShowBottom) {//圆圈在上 文字在下
        [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top).offset(0);
            make.width.height.mas_equalTo(24);
            make.centerX.mas_equalTo(self.contentView);
        }];
        [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.circleView.mas_bottom).offset(5);
            make.height.mas_equalTo(13);
            make.left.mas_equalTo(self.contentView.mas_left).offset(2);
            make.right.mas_equalTo(self.contentView.mas_right).offset(2);
        }];
    }else{//圆圈在左 文字在右边
        if (!self.lab.hidden) {
            [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.mas_equalTo(self.contentView);
                make.width.height.mas_equalTo(24);
               make.top.mas_equalTo(self.contentView.mas_top).offset(0);
            }];
            [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.mas_equalTo(self.circleView.mas_right).offset(10);
                make.right.mas_equalTo(self.contentView.mas_right).offset(2);
                make.centerY.mas_equalTo(self.circleView.mas_centerY);
            }];
        }
    }
}

#pragma mark - Animations
- (void)stopAnimation {
    // 设置layer的动画速度为0则停止动画
    self.refreshLayer.speed = 0.0;
    [self.refreshLayer removeAnimationForKey:@"rotationAnimation"];
}

- (void)refreshStateIdleAnimation{
    CABasicAnimation *scaleAnimation = (CABasicAnimation *)[self.refreshLayer animationForKey:@"scaleAnimation"];
    // 如果存在动画则不继续执行
    if (scaleAnimation) return;
    self.refreshLayer.speed = 1;
    [self.refreshLayer addAnimation:self.scaleAnimation forKey:@"scaleAnimation"];
}

- (void)refreshStateIdledSmallAnimation{
    CABasicAnimation *scaleAnimation = (CABasicAnimation *)[self.refreshLayer animationForKey:@"scaleSmallAnimation"];
    // 如果存在动画则不继续执行
    if (scaleAnimation) return;
    self.refreshLayer.speed = 1;
    [self.refreshLayer addAnimation:self.scalesSmallAnimation forKey:@"scalesSmallAnimation"];
}

- (void)refreshStateRefreshingAnimation{
    CABasicAnimation *rotationAnimation = (CABasicAnimation *)[self.refreshLayer animationForKey:@"rotationAnimation"];
    if (rotationAnimation) {
        return;
    }
    self.refreshLayer.speed = 1;
    [self.refreshLayer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];
}

#pragma mark - lazy

- (void)setRefreshTextShowBottom:(BOOL)refreshTextShowBottom{
    _refreshTextShowBottom = refreshTextShowBottom;
}

- (CALayer *)refreshLayer{
    if (!_refreshLayer) {
        _refreshLayer = [CALayer layer];
        _refreshLayer.backgroundColor = self.tintColor.CGColor; //圆环底色
        _refreshLayer.frame = CGRectMake(0, 0, 24, 24);
        //创建一个圆环
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(12, 12) radius:10 startAngle:0 endAngle:M_PI*2 clockwise:YES];
        
        //圆环遮罩
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.strokeColor = self.tintColor.CGColor;
        shapeLayer.lineWidth = 2;
        shapeLayer.strokeStart = 0;
        shapeLayer.strokeEnd = 0.8;
        shapeLayer.lineCap = @"round";
        shapeLayer.lineDashPhase = 0.8;
        shapeLayer.path = bezierPath.CGPath;
        
        //颜色渐变
        NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)self.tintColor.CGColor,(id)[[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor,(id)[[UIColor clearColor] colorWithAlphaComponent:0.5].CGColor, nil];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.shadowPath = bezierPath.CGPath;
        gradientLayer.frame = CGRectMake(0, 0, 30, 30);
        gradientLayer.locations = @[@(0.2),@(0.8),@(0.5)];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        [gradientLayer setColors:[NSArray arrayWithArray:colors]];
        [_refreshLayer addSublayer:gradientLayer]; //设置颜色渐变
        [_refreshLayer setMask:shapeLayer]; //设置圆环遮罩
    }
    return _refreshLayer;
}

- (CABasicAnimation *)scaleAnimation{
    if (!_scaleAnimation) {
        _scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        _scaleAnimation.autoreverses = NO;
//        _scaleAnimation.fillMode = kCAFillModeForwards;
        _scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        _scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
        _scaleAnimation.duration = 0.01;
    }
    return _scaleAnimation;
}

- (CABasicAnimation *)scalesSmallAnimation{
    if (!_scalesSmallAnimation) {
        _scalesSmallAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        _scalesSmallAnimation.autoreverses = NO;
        _scalesSmallAnimation.fromValue = [NSNumber numberWithFloat:1.0];
        _scalesSmallAnimation.toValue = [NSNumber numberWithFloat:0.0];
        _scalesSmallAnimation.duration = 0.2;
    }
    return _scalesSmallAnimation;
}

- (CABasicAnimation *)rotationAnimation{
    if (!_rotationAnimation) {
        _rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        _rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
        _rotationAnimation.toValue = [NSNumber numberWithFloat:6.0*M_PI];
        _rotationAnimation.autoreverses = NO;
        _rotationAnimation.fillMode = kCAFillModeForwards;
        _rotationAnimation.repeatCount = MAXFLOAT;
        _rotationAnimation.beginTime = 0.8; //延时执行，注释掉动画会同时进行
        _rotationAnimation.duration = 3;
    }
    return _rotationAnimation;
}
- (UILabel *)lab{
    if (!_lab) {//WithFrame:CGRectMake(34, 0, 100, 24)
        _lab = [[UILabel alloc] init];
        _lab.textColor = [UIColor colorWithHex:0xAAAAAA];
        _lab.text = @"加载中";
        _lab.font = [UIFont systemFontOfSize:12.0];
    }
    return _lab;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

- (UIView *)circleView{
    if (!_circleView) {
        _circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
        _circleView.backgroundColor = [UIColor clearColor];
    }
    return _circleView;
}

@end
